package com.engine.kq.wfset.util;

import com.engine.kq.biz.*;
import com.engine.kq.entity.KQDutyRulesDetailEntity;
import com.engine.kq.entity.KQOvertimeRulesDetailEntity;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import com.google.common.collect.Maps;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SplitDutyActionUtil extends BaseBean {
    public static KQLog kqLog = new KQLog();

    private static BaseBean baseBean = new BaseBean();


    /**
     * 生成值班数据
     *
     * @param splitBeans
     * @param requestid
     */
    public static void handleOverTimeAction(List<SplitBean> splitBeans, String requestid ) throws Exception {
//    保证同一个requestid能重复生成调休和值班
        String testval = "";

        KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
        RecordSet rs = new RecordSet();
        List<String> overKeys = new ArrayList<>();
        for (int i = 0; i < splitBeans.size(); i++) {
            SplitBean splitBean = splitBeans.get(i);
            String resourceId = splitBean.getResourceId();
            String belongDate = splitBean.getBelongDate();
            String toDate = splitBean.getToDate();
            String duration = splitBean.getDuration();
            String durationrule = splitBean.getDurationrule();

            int computingMode = Util.getIntValue(splitBean.getComputingMode());
            int changeType = splitBean.getChangeType();
            String paidSql = "insert into KQ_PAID_VACATION (requestId,workflowId,dataid,detailid,resourceId,fromDate,fromTime,toDate,toTime,duration,durationrule,computingMode,changeType)" +
                    " values(?,?,?,?,?,?,?,?,?,?,?,?,?) ";
            boolean isLog = rs.executeUpdate(paidSql, splitBean.getRequestId(), splitBean.getWorkflowId(),
                    splitBean.getDataId(), splitBean.getDetailId(), splitBean.getResourceId(),
                    splitBean.getFromDate(), splitBean.getFromTime(), splitBean.getToDate(),
                    splitBean.getToTime(), splitBean.getDuration(), splitBean.getDurationrule(), computingMode, changeType);
            if (isLog) {
                kqLog.info("值班生成调休:resourceId:" + resourceId + ":computingMode:" + computingMode + ":belongDate:" + belongDate + ":duration:" + duration + ":durationrule:" + durationrule + ":requestid:" + requestid);
                if (computingMode == 1) {
                    doComputingMode1_4TX(splitBean);
                }
            }
        }
    }


    /**
     * 值班方式是第一种，如果是先生成调休，后面才生成值班，需要先把调休给按照值班规则给生成出来
     *
     * @param splitBean
     */
    public static void doComputingMode1_4TX(SplitBean splitBean) {
        RecordSet rs = new RecordSet();
        int changeType = 2;//splitBean.getChangeType();
        String resourceId = splitBean.getResourceId();
        String belongDate = splitBean.getBelongDate();
        String duration = splitBean.getDuration();
        String fromdatedb = splitBean.getFromdatedb();
        String fromtimedb = splitBean.getFromtimedb();
        String todatedb = splitBean.getTodatedb();
        String totimedb = splitBean.getTotimedb();
        String requestId = splitBean.getRequestId();
        String fromdate = splitBean.getFromDate();
        String fromtime = splitBean.getFromTime();
        String todate = splitBean.getToDate();
        String totime = splitBean.getToTime();
        String duty_type = splitBean.getDuty_type();
        double D_Mins = splitBean.getD_Mins();
        KQDutyRuleCalBiz kqDutyRuleCalBiz = new KQDutyRuleCalBiz();

        //   根据值班单位重新生成下值班分钟数
        D_Mins = kqDutyRuleCalBiz.getD_MinsByUnit(D_Mins);

        Map<String, Object> otherParam = Maps.newHashMap();
        otherParam.put("duty_type", splitBean.getDuty_type());
        String tiaoxiuId = KQDutyBiz.addExtraAmountByDis9(resourceId, belongDate, D_Mins + "", "0" , requestId, "1", fromdatedb, otherParam);
        if (Util.getIntValue(tiaoxiuId) > 0) {
            int unit = KQDutyRulesBiz.getMinimumUnit();
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Map<String,Integer> changeTypeMap = Maps.newHashMap();
            Map<String, KQDutyRulesDetailEntity> overRulesDetailMap = Maps.newHashMap();
            Map<String, List<String[]>> restTimeMap = Maps.newHashMap();
            Map<String,Integer> computingModeMap = Maps.newHashMap();
            String changeType_key = belongDate + "_" + changeType;

            kqDutyRuleCalBiz.getDutyDataMap(resourceId, belongDate, belongDate, dateFormatter,changeTypeMap,overRulesDetailMap,restTimeMap,computingModeMap);

            KQDutyRulesDetailEntity kqDutyRulesDetailEntity = overRulesDetailMap.get(changeType_key);
            int paidLeaveEnable = kqDutyRuleCalBiz.getPaidLeaveEnable(kqDutyRulesDetailEntity, duty_type);

            String flow_duty_sql = "insert into kq_flow_duty(requestid,resourceid,fromdate,fromtime,todate,totime,duration_min,expiringdate,belongdate,workMins,durationrule,changetype,paidLeaveEnable,computingMode,fromdatedb,fromtimedb,todatedb,totimedb,tiaoxiuid)"+
                    " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
            boolean isUp = rs.executeUpdate(flow_duty_sql, requestId,resourceId,fromdate,fromtime,todate,totime,D_Mins,"",belongDate,"",
                    unit,changeType,paidLeaveEnable,"1",fromdatedb,fromtimedb,todatedb,totimedb,tiaoxiuId);
            if(!isUp){
                kqLog.info("doComputingMode1 加班数据flow_overtime_sql记录失败！！！");
            }else{
                kqLog.info("doComputingMode1:flow_overtime_sql: "+flow_duty_sql);
                kqLog.info("doComputingMode1:requestId:"+requestId+":resourceId:"+resourceId+":fromdate:"+fromdate+":fromtime:"+fromtime
                        +":todate:"+todate+":totime:"+totime+":D_Mins:"+D_Mins+":belongDate:"+belongDate+":unit:"+unit+":changeType:"+changeType
                        +":paidLeaveEnable:"+paidLeaveEnable+":fromdateDB:"+fromdatedb+":fromtimedb:"+fromtimedb+":todatedb:"+todatedb+":totimedb:"+totimedb);
            }

            kqLog.info("doComputingMode1 值班生成调休成功！！！");
        } else {
            kqLog.info("doComputingMode1 值班生成调休失败！！！");
        }


    }
}
